package net.csdra.user.component;

import net.csdra.basis.Utils;
import net.csdra.basis.service.CurrentUserInfo;
import net.csdra.basis.service.ServiceBaseInterfaceImpl;
import net.csdra.user.domain.User;
import net.csdra.user.domain.UserRepository;
import net.csdra.user.component.form.CreateUser;
import net.csdra.user.component.form.UpdateUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.Assert;

/**
 * Created by msf on 2018/1/25.
 */
public class UserComponentImpl extends ServiceBaseInterfaceImpl<User, CreateUser, UpdateUser> implements UserComponent {

    UserRepository userRepository;
    CurrentUserInfo<User> userCurrentUserInfo;

    public UserComponentImpl(UserRepository userRepository, CurrentUserInfo<User> userCurrentUserInfo) {
        super(User.class, userRepository, userCurrentUserInfo);
        this.userRepository = userRepository;
        this.userCurrentUserInfo = userCurrentUserInfo;
    }

//    @Override
//    public User create(CreateUser createUser) {
//        //TODO 补充断言
//        /// RETODO 完成
//        Assert.notNull(createUser, "传入参数为空");
//        Assert.hasText(createUser.getMetaId(), "metaId为空");
//        Assert.hasText(createUser.getUserName(), "userName为空");
//        Assert.notNull(createUser.getPassword(), "password为空");
//        User user = Utils.createAutoSet(User.class, this.userCurrentUserInfo.get().getUserName());
//        //TODO 补充剩余user属性
//        // RETODO 完成
//        user.setMetaId(createUser.getMetaId());
//        user.setUserName(createUser.getUserName());
//        user.setPassword(createUser.getPassword().toString());
//        user.setCustomize(createUser.getCustomize());
//        userRepository.save(user);
//        User result = new User();
//        Utils.copyProperties(user, result);
//        return result;
//    }

//    @Override
//    public User update(UpdateUser updateUser) {
//        //TODO 补充断言
//        // RETODO 完成
//        Assert.notNull(updateUser, "传入参数为空");
//        User user = this.userRepository.findOne(updateUser.getId());
//        Assert.notNull(user, "找不到该user");
//        Assert.notNull(user.getPassword(), "password为空");
//        Utils.updateAutoSet(user, this.userCurrentUserInfo.get().getUserName());
//        //TODO 补充剩余user属性
//        // RETODO 完成
//        user.setPassword(updateUser.getPassword().toString());
//        user.setCustomize(updateUser.getCustomize());
//        user.setEnabled(updateUser.getEnabled());
//        user.setOrder(updateUser.getOrder());
//        userRepository.save(user);
//        User result = new User();
//        Utils.copyProperties(user, result);
//        return result;
//    }

    @Override
    public User getUserByUserNameAndPassWord(String userName, CharSequence password) {
        User user = userRepository.findByUserNameAndPassword(userName, password.toString());
        return user;
    }

    @Override
    public User getUserByUserNameAndTemporaryPwd(String userName, CharSequence temporaryPwd) {
        User user = userRepository.findByUserNameAndTemporaryPwd(userName, temporaryPwd.toString());
        return user;
    }

    @Override
    public Page<User> findPageByMetaIdAndAuthoritys(String metaId, String authoritys, Pageable pageable) {
        Page<User> userPage = userRepository.findByMetaIdAndAuthoritys(metaId, authoritys, pageable);
        return userPage;
    }
}
